import { request } from "../../request/index.js";
// import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  data: {
    goodsObj: {},
    //商品是否被收藏过
    isCollect: false
  },
  //商品图片
  GoodsInfo: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options;

    const { goods_id } = options;
    this.getGoodsDetail(goods_id);
  },
  //获取商品详情数据
  async getGoodsDetail(goods_id) {
    const goodsObj = await request({ url: "/goods/detail", data: { goods_id } });
    this.GoodsInfo = goodsObj;

    //1.获取缓存中的商品收藏的数组
    let collect = wx.getStorageSync("collect") || [];
    //2.判断当前商品是否被收藏
    //some函数：当全部为true。。（忘记了）
    let isCollect = collect.some(v => v.goods_id === this.GoodsInfo.goods_id);

    this.setData({
      goodsObj: {
        goods_name: goodsObj.goods_name,
        goods_price: goodsObj.goods_price,
        // iphone部分手机 不识别 .webp 图片格式
        // 最好找到后台，让他修改。实在没办法淋湿自己改，确保后台存在：.webp --> .jpg
        goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, '.jpg'),
        pics: goodsObj.pics
      },
      isCollect
    })
  },
  //点击轮播图，放大预览
  handlePrevewImage(e) {
    //1.先构造要预览的图片数组
    const urls = this.GoodsInfo.pics.map(v => v.pics_mid);
    // 接收传递过来的图片url
    const current = e.currentTarget.dataset.url;
    wx.previewImage({
      //点击事件触发
      current,
      urls
    });
  },
  //点击，加入购物车
  handleCartAdd() {
    //1.获取缓存中的购物车 数组
    let cart = wx.getStorageSync("cart") || [];
    //2.判断 商品对象是否存在于购物车数组中
    let index = cart.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
    if (index === -1) {
      //3.不存在 第一次添加
      this.GoodsInfo.num = 1;
      //商品的选中状态
      this.GoodsInfo.checked = true;
      cart.push(this.GoodsInfo);
    } else {
      //4.已存在购物车数据，执行num++
      cart[index].num++;
    }
    //5.把购物车重新添加到缓存中
    wx.setStorageSync("cart", cart);
    //6.弹窗提示
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      // true放置用户 手抖，疯狂点击按钮
      mask: true,
    });

  },

  //点击 商品收藏图标
  handleCollect() {
    let isCollect = false;
    //1.获取缓存中的商品收藏数组
    let collect = wx.getStorageSync("collect") || [];
    //2.判断该商品是否被收藏过
    let index = collect.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
    //3.当index！==-1，表示已经被收藏过
    if (index !== -1) {
      //能找到，已经收藏过，在数组中删除该商品
      collect.splice(index, 1);
      isCollect = false;
      wx.showToast({
        title: '取消成功',
        icon: 'success',
        mask: true
      })
    } else {
      //没有收藏过
      collect.push(this.GoodsInfo);
      isCollect = true;
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        mask: true
      })
    }
    //4.把数组存入到缓存中
    wx.setStorageSync("collect", collect);
    //5.修改data中的值
    this.setData({ isCollect })
  }

})